package com.greenold.Appointment.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.greenold.common.annotation.Log;
import com.greenold.common.core.controller.BaseController;
import com.greenold.common.core.domain.AjaxResult;
import com.greenold.common.enums.BusinessType;
import com.greenold.Appointment.domain.Appointment;
import com.greenold.Appointment.service.IAppointmentService;
import com.greenold.common.utils.poi.ExcelUtil;
import com.greenold.common.core.page.TableDataInfo;

/**
 * 预约登记Controller
 * 
 * @author ruoyi
 * @date 2025-10-13
 */
@RestController
    @RequestMapping("/appointment/appointment")
public class AppointmentController extends BaseController
{
    @Autowired
    private IAppointmentService appointmentService;

    /**
     * 根据userId改变取消次数的值（extend1）
     */
//    @PreAuthorize("@ss.hasPermi('appointment:appointment:updateCancelCountByUserId')")
//    @Log(title = "根据userId改变取消次数的值（extend1）", businessType = BusinessType.UPDATE)
    @PutMapping("/updateCancelCountByUserId/{userId}")
    public AjaxResult updateCancelCountByUserId(@PathVariable("userId") String userId)
    {
        return toAjax(appointmentService.updateCancelCountByUserId(userId));
    }
    /**
     * 根据userId查询取消次数（extend1）
     */
//    @PreAuthorize("@ss.hasPermi('appointment:appointment:selectCancelCountByUserId')")
    @GetMapping("/selectCancelCountByUserId/{userId}")
    public Map<String, Object> selectCancelCountByUserId(@PathVariable("userId") String userId)
    {
        Integer cancelCount = appointmentService.selectCancelCountByUserId(userId);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("cancelCount",cancelCount);
        return map;
    }
    /**
     * 根据userId查询更新日期（updatedAt）
     */
//    @PreAuthorize("@ss.hasPermi('appointment:appointment:selectUpdatedAtByUserId')")
    @GetMapping("/selectUpdatedAtByUserId/{userId}")
    public Map<String,Object> selectUpdatedAtByUserId(@PathVariable("userId") String userId)
    {
        Date updatedAt = appointmentService.selectUpdatedAtByUserId(userId);
        Map<String,Object> map = new HashMap<String,Object>();
        if(updatedAt!=null)
        {
            map.put("msg","该用户最近一次更新时间为："+updatedAt);
        }
        else
        {
            map.put("msg","该用户未更新");
        }
        map.put("updatedAt",updatedAt);
        return map;
    }
    /**
     * 清空取消次数（extend1）
     */
//    @PreAuthorize("@ss.hasPermi('appointment:appointment:updateCancelCountToZeroByUserId')")
//    @Log(title = "根据userId清空取消次数（extend1）", businessType = BusinessType.UPDATE)
    @PutMapping("/zero")
    public AjaxResult updateCancelCountToZeroByUserId()
    {
        appointmentService.updateCancelCountToZero();
        return toAjax(1);
    }
    /**
     * 查询预约登记列表
     */
//    @PreAuthorize("@ss.hasPermi('appointment:appointment:list')")
    @GetMapping("/list")
    public TableDataInfo list(Appointment appointment)
    {
        startPage();
        List<Appointment> list = appointmentService.selectAppointmentList(appointment);
        return getDataTable(list);
    }
    /**根据传入的手机号查询预约表中是否有记录
     * */
    @GetMapping("/checkMobile/{mobile}")
    public Map<String,Object> checkMobile(@PathVariable("mobile") String mobile)
    {
        Integer count = appointmentService.selectAppointmentByMobile(mobile);
        Map<String,Object> map = new HashMap<String,Object>();
        if(count>0)
        {
            map.put("msg","该手机号已预约");
        }
        else
        {
            map.put("msg","该手机号未预约");
        }
        map.put("count",count);
        return map;
    }
    /**根据传入的userId查询绑定的老人姓名
     * */
    @GetMapping("/elderName/{userId}")
    public Map<String,Object> elderName(@PathVariable("userId") String userId)
    {
        List<String> name = appointmentService.selectElderNameByUserId(userId);
        Map<String,Object> map = new HashMap<String,Object>();
        if(name!=null)
        {
            map.put("msg","该用户绑定的老人姓名为："+name);
        }
        else
        {
            map.put("msg","该用户未绑定老人");
        }
        map.put("name",name);
        return map;
    }
    /**根据传入的日期查询预约登记列表数量
     * */
    @GetMapping("/count/{date}")
    public Map<String,Object> count(@PathVariable("date") String date)
    {
        Integer count = appointmentService.selectAppointmentCountByDate(date);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("msg","该日期预约登记数量为："+count);
        map.put("count",count);
        return map;
    }
    /**
     * 导出预约登记列表
     */
    @PreAuthorize("@ss.hasPermi('appointment:appointment:export')")
    @Log(title = "预约登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Appointment appointment)
    {
        List<Appointment> list = appointmentService.selectAppointmentList(appointment);
        ExcelUtil<Appointment> util = new ExcelUtil<Appointment>(Appointment.class);
        util.exportExcel(response, list, "预约登记数据");
    }

    /**
     * 获取预约登记详细信息
     */
    @PreAuthorize("@ss.hasPermi('appointment:appointment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(appointmentService.selectAppointmentById(id));
    }

    /**
     * 新增预约登记
     */
//    @PreAuthorize("@ss.hasPermi('appointment:appointment:add')")
    @Log(title = "预约登记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Appointment appointment)
    {
        return toAjax(appointmentService.insertAppointment(appointment));
    }

    /**
     * 修改预约登记
     */
//    @PreAuthorize("@ss.hasPermi('appointment:appointment:edit')")
    @Log(title = "预约登记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Appointment appointment)
    {
        return toAjax(appointmentService.updateAppointment(appointment));
    }

    /**
     * 删除预约登记
     */
    @PreAuthorize("@ss.hasPermi('appointment:appointment:remove')")
    @Log(title = "预约登记", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(appointmentService.deleteAppointmentByIds(ids));
    }
}
